package BehaviorPattern.Mediator;

/**
 * @author: Themberfue
 * @date: 2025/8/13 12:04
 * @description:
 */
import java.util.HashMap;
import java.util.Map;

// 1. 中介者接口：聊天室
interface ChatMediator {
    void registerUser(User user);  // 注册用户
    void sendMessage(String message, User sender);  // 发送消息
}

// 2. 具体中介者：具体聊天室
class ChatRoom implements ChatMediator {
    private Map<String, User> users;  // 存储所有用户

    public ChatRoom() {
        this.users = new HashMap<>();
    }

    @Override
    public void registerUser(User user) {
        users.put(user.getName(), user);
        user.setMediator(this);  // 设置用户的中介者
        System.out.println(user.getName() + "加入了聊天室");
    }

    @Override
    public void sendMessage(String message, User sender) {
        // 向聊天室中除发送者外的所有用户转发消息
        for (User user : users.values()) {
            if (!user.equals(sender)) {
                user.receiveMessage(message, sender.getName());
            }
        }
    }
}

// 3. 同事接口：用户
abstract class User {
    protected String name;
    protected ChatMediator mediator;  // 持有中介者引用

    public User(String name) {
        this.name = name;
    }

    public void setMediator(ChatMediator mediator) {
        this.mediator = mediator;
    }

    public String getName() {
        return name;
    }

    public abstract void sendMessage(String message);  // 发送消息
    public abstract void receiveMessage(String message, String sender);  // 接收消息
}

// 4. 具体同事：普通用户
class RegularUser extends User {
    public RegularUser(String name) {
        super(name);
    }

    @Override
    public void sendMessage(String message) {
        System.out.println(name + "发送消息: " + message);
        mediator.sendMessage(message, this);  // 通过中介者发送
    }

    @Override
    public void receiveMessage(String message, String sender) {
        System.out.println(name + "收到来自" + sender + "的消息: " + message);
    }
}

// 4. 具体同事：管理员
class AdminUser extends User {
    public AdminUser(String name) {
        super(name);
    }

    @Override
    public void sendMessage(String message) {
        System.out.println("[管理员]" + name + "发送消息: " + message);
        mediator.sendMessage(message, this);  // 通过中介者发送
    }

    @Override
    public void receiveMessage(String message, String sender) {
        System.out.println("[管理员]" + name + "收到来自" + sender + "的消息: " + message);
    }
}

// 5. 客户端代码
public class MediatorExample {
    public static void main(String[] args) {
        // 创建中介者：聊天室
        ChatMediator chatRoom = new ChatRoom();

        // 创建同事：用户
        User alice = new RegularUser("Alice");
        User bob = new RegularUser("Bob");
        User charlie = new RegularUser("Charlie");
        User admin = new AdminUser("Admin");

        // 注册用户到聊天室
        chatRoom.registerUser(alice);
        chatRoom.registerUser(bob);
        chatRoom.registerUser(charlie);
        chatRoom.registerUser(admin);

        // 发送消息（通过中介者转发）
        System.out.println("\n=== 开始聊天 ===");
        alice.sendMessage("大家好！");
        System.out.println();
        bob.sendMessage("你好，Alice！");
        System.out.println();
        admin.sendMessage("欢迎大家加入聊天室，请遵守规则");
    }
}

